package cn.edu.tju.wallet.service;

import cn.edu.tju.wallet.domain.VirtualWallet;
import cn.edu.tju.wallet.dto.WalletBalanceDTO;
import cn.edu.tju.wallet.repository.VirtualWalletRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WalletQueryService {

    @Autowired
    private VirtualWalletRepository walletRepository;

    /**
     * 查询余额（完全遵循指南代码）
     */
    public WalletBalanceDTO getBalance(Long userId) {
        VirtualWallet wallet = walletRepository.findByUserId(userId)
                .orElseThrow(() -> new RuntimeException("钱包不存在"));

        return new WalletBalanceDTO(
                wallet.getId(),
                wallet.getBalance(),
                wallet.getAvailableBalance(),
                wallet.getFrozenBalance(),
                wallet.getPrincipalBalance(),
                wallet.getBonusBalance(),
                wallet.getOverdraftAmount(),
                wallet.getOverdraftLimit()
        );
    }
}